package net.expectx.pay.controller;

import io.swagger.annotations.ApiOperation;
import net.expectx.common.single.base.BaseConstants;
import net.expectx.common.single.base.BaseResult;
import net.expectx.pay.common.AdminConstants;
import net.expectx.pay.common.AdminResult;
import net.expectx.pay.common.util.ExpectXUtil;
import net.expectx.pay.dao.bean.ContentNotification;
import net.expectx.pay.dao.bean.ContentNotificationExample;
import net.expectx.pay.rpc.api.ContentNotificationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lijian
 */
@Controller
@RequestMapping(value = "/manager/contentNotification")
public class ContentNotificationController  {
    @Autowired
    ContentNotificationService contentNotificationService;

    @ApiOperation(value = "公告主页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "contentNotification/index";
    }

    @ApiOperation(value = "新增公告")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(ModelMap modelMap) {
        return "contentNotification/modify";
    }

    @ApiOperation(value = "修改公告")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap) {
        ContentNotificationExample contentNotificationExample = new ContentNotificationExample();
        ContentNotificationExample.Criteria criteria = contentNotificationExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        criteria.andIdEqualTo(id);
        ContentNotification contentNotification = contentNotificationService.selectByExample(contentNotificationExample).get(0);
        modelMap.put("contentNotification", contentNotification);
        return "contentNotification/modify";
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Object page(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request) {

        ContentNotificationExample contentNotificationExample = new ContentNotificationExample();
        ContentNotificationExample.Criteria criteria = contentNotificationExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        String name = request.getParameter("name");
        if (StringUtils.isNotBlank(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        contentNotificationExample.setPageIndex(page);
        contentNotificationExample.setPageSize(limit);
        contentNotificationExample.setOrderByClause("gmt_modified desc");

        List<ContentNotification> contentCategories = contentNotificationService.selectByExample(contentNotificationExample);
        long total = contentNotificationService.countByExample(contentNotificationExample);
        Map<String, Object> result = new HashMap<>(16);
        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", contentCategories);
        return result;
    }

    @ApiOperation(value = "新增")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(ContentNotification contentNotification) {
        contentNotification.setGmtCreate(System.currentTimeMillis());
        contentNotification.setGmtModified(System.currentTimeMillis());
        contentNotification.setIsDeleted(BaseConstants.NO);
        return contentNotificationService.insertSelective(contentNotification);
    }


    @ApiOperation(value = "修改公告")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Long id, ContentNotification contentNotification) {
        contentNotification.setGmtModified(System.currentTimeMillis());
        return contentNotificationService.updateByPrimaryKeySelective(contentNotification);
    }

    @ApiOperation(value = "删除公告")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        ContentNotificationExample contentNotificationExample = new ContentNotificationExample();
        contentNotificationExample.createCriteria().andIdIn(ExpectXUtil.toList(ids, ","));
        BaseResult baseResult = contentNotificationService.physicalRemoveByExample(AdminConstants.YES, contentNotificationExample);
        return new AdminResult(baseResult);
    }
}
